package cn.koala.key.receipt.service.impl;

import cn.koala.key.api.KeyManagementApi;
import cn.koala.key.common.excel.FileUtil;
import cn.koala.key.common.feign.FeignUtils;
import cn.koala.key.entity.BorrowOrder;
import cn.koala.key.receipt.dto.BorrowOrderCheckDTO;
import cn.koala.key.receipt.dto.BorrowOrderDTO;
import cn.koala.key.receipt.dto.BorrowOrderImportDTO;
import cn.koala.key.receipt.dto.BorrowOrderSearchDTO;
import cn.koala.key.receipt.service.BorrowOrderService;
import cn.koala.key.stock.domain.dto.NewCarDTO;
import cn.seed.common.core.*;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@Service
public class BorrowOrderServiceImpl implements BorrowOrderService {

    @Override
    public Result submit(Long id) {
        return FeignUtils.getHttpApi(KeyManagementApi.class).submit(id);
    }

    @Override
    public Result invalid(Long id) {
        return FeignUtils.getHttpApi(KeyManagementApi.class).invalid(id);
    }

    @Override
    public Result check(List<BorrowOrderCheckDTO> borrowOrderCheckList) {
        return FeignUtils.getHttpApi(KeyManagementApi.class).check(borrowOrderCheckList);
    }

    @Override
    public Result<BorrowOrder> getById(Long id) {
        return FeignUtils.getHttpApi(KeyManagementApi.class).getById(id);
    }

    @Override
    public Result createOrEdit(BorrowOrderDTO borrowOrderDTO) {
        return FeignUtils.getHttpApi(KeyManagementApi.class).createOrEdit(borrowOrderDTO);
    }

    @Override
    public Result<List<BorrowOrder>> batchCreate(MultipartFile file, String userCode) {
        List<BorrowOrderImportDTO> borrowOrderImportList = null;
        try {
            borrowOrderImportList = FileUtil.importExcel(file, 1, 1, BorrowOrderImportDTO.class);
        } catch (Exception e) {
            return ResultGenerator.genFailResult(ResultCode.FAIL, "导入excel异常！");
        }
        if(null == borrowOrderImportList || borrowOrderImportList.isEmpty()){
            throw new ServiceException(ResultCode.BAD_REQUEST,  "excel数据异常请检查！！！");
        }

        List<BorrowOrderDTO> orderList = new ArrayList<>();
        for (BorrowOrderImportDTO borrowOrderImportDTO : borrowOrderImportList) {
            orderList.add(borrowOrderImportDTO.convertBorrowOrderDTO(userCode));
        }

        return FeignUtils.getHttpApi(KeyManagementApi.class).importBorrowOrder(orderList);
    }

    /**
     * @Description: 钥匙管理的总部账号可以查所有来源的订单，并且总部账号只能查看 未确认，已借出，归还中，已完成 的单子
     *               考拉门店，一嗨门店，企业等门店账号只能查询各自的借用单，并且只能查到 未提交，未审核，已审核，已拒绝，已借出，已作废, 已完成 的单子
     * 
     * @param
     * @return Object
     * @throws
     * @exception:
     * @author: 32415
     * @time: 2019/5/7 15:18
     */
    @Override
    public Result<ResultList<BorrowOrder>> searchBorrowOrder(BorrowOrderSearchDTO borrowOrderSearch) {
        return FeignUtils.getHttpApi(KeyManagementApi.class).searchBorrowOrder(borrowOrderSearch);
    }
}
